package com.gmrz.uaf.protocol.v1.processor;

import com.gmrz.uaf.db.DAOException;
import com.gmrz.uaf.db.UAFDAOFactory;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uaf.db.dao.AuthenticatorDAO;
import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
import com.gmrz.uaf.protocol.v1.schema.AuthenticatorRegList;
import com.gmrz.util.db.DBUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @PACKAGE_NAME: com.gmrz.uaf.protocol.v1.processor
 * @ClassName: UAFQueryRegListProcessor
 * @Description:  根据用户名、渠道号等查询用户注册信息 Processor
 * @Date: 2020-04-29 14:45
 * @Author: liyl@gmrz-bj.com
 **/
public class UAFQueryRegListProcessor extends UAFBaseProcessor {

    private static final Logger LOG = LogManager.getLogger(UAFQueryRegListProcessor.class);

    public List<AuthenticatorRegList> queryRegListByUserNameAndTenantID(String userName, String appId,
                                                                        String transType, String authType) throws DAOException {
        Connection conn = null;
        List<AuthenticatorRegList> authenticators;
        try {

            conn = UAFDBConnectionMgr.getConnection(true);
            AuthenticatorDAO authenticatorDAO = UAFDAOFactory.createAuthenticatorDAO(conn);
            authenticators = authenticatorDAO.queryRegListByUseNameAndTenantID(userName, appId, transType, authType);

        }catch (SQLException e){
            LOG.error("Failed to access t_authenticators data, ", e);
            throw new DAOException(UAFErrorCode.DB_DATABASE_CONNECTION_ERROR, e);
        }finally {
            DBUtil.close(conn);
        }

        return authenticators;
    }
}
